package com.study.javaOther.bianli;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @Name: MainTest
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/7/21 17:43
 * @Remark: 特殊说明
 * @Version: 1.0
 */
public class MainTest {
    public static void main(String[] args) {
        //region 数据
        String json="[\n" +
                "  {\n" +
                "    \"id\": \"1123598813738675201\",\n" +
                "    \"createUser\": -1,\n" +
                "    \"createDept\": -1,\n" +
                "    \"createTime\": \"\",\n" +
                "    \"updateUser\": -1,\n" +
                "    \"updateTime\": \"\",\n" +
                "    \"status\": -1,\n" +
                "    \"isDeleted\": -1,\n" +
                "    \"parentId\": \"0\",\n" +
                "    \"title\": \"WD\",\n" +
                "    \"key\": \"1123598813738675201\",\n" +
                "    \"value\": \"1123598813738675201\",\n" +
                "    \"disabled\": true,\n" +
                "    \"isLeaf\": false,\n" +
                "    \"hasChildren\": true,\n" +
                "    \"children\": [\n" +
                "      {\n" +
                "        \"id\": \"1123598813738675202\",\n" +
                "        \"createUser\": -1,\n" +
                "        \"createDept\": -1,\n" +
                "        \"createTime\": \"\",\n" +
                "        \"updateUser\": -1,\n" +
                "        \"updateTime\": \"\",\n" +
                "        \"status\": -1,\n" +
                "        \"isDeleted\": -1,\n" +
                "        \"parentId\": \"1123598813738675201\",\n" +
                "        \"title\": \"开发组\",\n" +
                "        \"key\": \"1123598813738675202\",\n" +
                "        \"value\": \"1123598813738675202\",\n" +
                "        \"disabled\": true,\n" +
                "        \"isLeaf\": false,\n" +
                "        \"hasChildren\": true,\n" +
                "        \"children\": [\n" +
                "          {\n" +
                "            \"id\": \"1520314014657748994\",\n" +
                "            \"createUser\": -1,\n" +
                "            \"createDept\": -1,\n" +
                "            \"createTime\": \"\",\n" +
                "            \"updateUser\": -1,\n" +
                "            \"updateTime\": \"\",\n" +
                "            \"status\": -1,\n" +
                "            \"isDeleted\": -1,\n" +
                "            \"parentId\": \"1123598813738675202\",\n" +
                "            \"title\": \"用户\",\n" +
                "            \"key\": \"1520314014657748994\",\n" +
                "            \"value\": \"1520314014657748994\",\n" +
                "            \"disabled\": false,\n" +
                "            \"isLeaf\": true,\n" +
                "            \"hasChildren\": false,\n" +
                "            \"parentName\": \"开发组\",\n" +
                "            \"deptId\": -1,\n" +
                "            \"deptRoleId\": -1,\n" +
                "            \"type\": \"\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"id\": \"1549954776734683137\",\n" +
                "            \"createUser\": -1,\n" +
                "            \"createDept\": -1,\n" +
                "            \"createTime\": \"\",\n" +
                "            \"updateUser\": -1,\n" +
                "            \"updateTime\": \"\",\n" +
                "            \"status\": -1,\n" +
                "            \"isDeleted\": -1,\n" +
                "            \"parentId\": \"1123598813738675202\",\n" +
                "            \"title\": \"0721\",\n" +
                "            \"key\": \"1549954776734683137\",\n" +
                "            \"value\": \"1549954776734683137\",\n" +
                "            \"disabled\": false,\n" +
                "            \"isLeaf\": true,\n" +
                "            \"hasChildren\": false,\n" +
                "            \"parentName\": \"开发组\",\n" +
                "            \"deptId\": -1,\n" +
                "            \"deptRoleId\": -1,\n" +
                "            \"type\": \"\"\n" +
                "          }\n" +
                "        ],\n" +
                "        \"parentName\": \"WD\",\n" +
                "        \"deptId\": -1,\n" +
                "        \"deptRoleId\": -1,\n" +
                "        \"type\": \"\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": \"1123598813738675203\",\n" +
                "        \"createUser\": -1,\n" +
                "        \"createDept\": -1,\n" +
                "        \"createTime\": \"\",\n" +
                "        \"updateUser\": -1,\n" +
                "        \"updateTime\": \"\",\n" +
                "        \"status\": -1,\n" +
                "        \"isDeleted\": -1,\n" +
                "        \"parentId\": \"1123598813738675201\",\n" +
                "        \"title\": \"产品组\",\n" +
                "        \"key\": \"1123598813738675203\",\n" +
                "        \"value\": \"1123598813738675203\",\n" +
                "        \"disabled\": true,\n" +
                "        \"isLeaf\": false,\n" +
                "        \"hasChildren\": true,\n" +
                "        \"children\": [\n" +
                "          {\n" +
                "            \"id\": \"1520314014657748994\",\n" +
                "            \"createUser\": -1,\n" +
                "            \"createDept\": -1,\n" +
                "            \"createTime\": \"\",\n" +
                "            \"updateUser\": -1,\n" +
                "            \"updateTime\": \"\",\n" +
                "            \"status\": -1,\n" +
                "            \"isDeleted\": -1,\n" +
                "            \"parentId\": \"1123598813738675203\",\n" +
                "            \"title\": \"用户\",\n" +
                "            \"key\": \"1520314014657748994\",\n" +
                "            \"value\": \"1520314014657748994\",\n" +
                "            \"disabled\": false,\n" +
                "            \"isLeaf\": true,\n" +
                "            \"hasChildren\": false,\n" +
                "            \"parentName\": \"产品组\",\n" +
                "            \"deptId\": -1,\n" +
                "            \"deptRoleId\": -1,\n" +
                "            \"type\": \"\"\n" +
                "          },\n" +
                "          {\n" +
                "            \"id\": \"1549954776734683137\",\n" +
                "            \"createUser\": -1,\n" +
                "            \"createDept\": -1,\n" +
                "            \"createTime\": \"\",\n" +
                "            \"updateUser\": -1,\n" +
                "            \"updateTime\": \"\",\n" +
                "            \"status\": -1,\n" +
                "            \"isDeleted\": -1,\n" +
                "            \"parentId\": \"1123598813738675203\",\n" +
                "            \"title\": \"0721\",\n" +
                "            \"key\": \"1549954776734683137\",\n" +
                "            \"value\": \"1549954776734683137\",\n" +
                "            \"disabled\": false,\n" +
                "            \"isLeaf\": true,\n" +
                "            \"hasChildren\": false,\n" +
                "            \"parentName\": \"产品组\",\n" +
                "            \"deptId\": -1,\n" +
                "            \"deptRoleId\": -1,\n" +
                "            \"type\": \"\"\n" +
                "          }\n" +
                "        ],\n" +
                "        \"parentName\": \"WD\",\n" +
                "        \"deptId\": -1,\n" +
                "        \"deptRoleId\": -1,\n" +
                "        \"type\": \"\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": \"1520311284799180802\",\n" +
                "        \"createUser\": -1,\n" +
                "        \"createDept\": -1,\n" +
                "        \"createTime\": \"\",\n" +
                "        \"updateUser\": -1,\n" +
                "        \"updateTime\": \"\",\n" +
                "        \"status\": -1,\n" +
                "        \"isDeleted\": -1,\n" +
                "        \"parentId\": \"1123598813738675201\",\n" +
                "        \"title\": \"项目组\",\n" +
                "        \"key\": \"1520311284799180802\",\n" +
                "        \"value\": \"1520311284799180802\",\n" +
                "        \"disabled\": true,\n" +
                "        \"isLeaf\": false,\n" +
                "        \"hasChildren\": true,\n" +
                "        \"children\": [\n" +
                "          {\n" +
                "            \"id\": \"1520314014657748994\",\n" +
                "            \"createUser\": -1,\n" +
                "            \"createDept\": -1,\n" +
                "            \"createTime\": \"\",\n" +
                "            \"updateUser\": -1,\n" +
                "            \"updateTime\": \"\",\n" +
                "            \"status\": -1,\n" +
                "            \"isDeleted\": -1,\n" +
                "            \"parentId\": \"1520311284799180802\",\n" +
                "            \"title\": \"用户\",\n" +
                "            \"key\": \"1520314014657748994\",\n" +
                "            \"value\": \"1520314014657748994\",\n" +
                "            \"disabled\": false,\n" +
                "            \"isLeaf\": true,\n" +
                "            \"hasChildren\": false,\n" +
                "            \"parentName\": \"项目组\",\n" +
                "            \"deptId\": -1,\n" +
                "            \"deptRoleId\": -1,\n" +
                "            \"type\": \"\"\n" +
                "          }\n" +
                "        ],\n" +
                "        \"parentName\": \"WD\",\n" +
                "        \"deptId\": -1,\n" +
                "        \"deptRoleId\": -1,\n" +
                "        \"type\": \"\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": \"1123598816738675201\",\n" +
                "        \"createUser\": -1,\n" +
                "        \"createDept\": -1,\n" +
                "        \"createTime\": \"\",\n" +
                "        \"updateUser\": -1,\n" +
                "        \"updateTime\": \"\",\n" +
                "        \"status\": -1,\n" +
                "        \"isDeleted\": -1,\n" +
                "        \"parentId\": \"1123598813738675201\",\n" +
                "        \"title\": \"超级管理员\",\n" +
                "        \"key\": \"1123598816738675201\",\n" +
                "        \"value\": \"1123598816738675201\",\n" +
                "        \"disabled\": false,\n" +
                "        \"isLeaf\": true,\n" +
                "        \"hasChildren\": false,\n" +
                "        \"parentName\": \"WD\",\n" +
                "        \"deptId\": -1,\n" +
                "        \"deptRoleId\": -1,\n" +
                "        \"type\": \"\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"parentName\": \"\",\n" +
                "    \"deptId\": -1,\n" +
                "    \"deptRoleId\": -1,\n" +
                "    \"type\": \"\"\n" +
                "  }\n" +
                "]\n";
        //endregion
        JSONArray jsonArray= JSON.parseArray(json);
        String id="0:1123598813738675201";
        System.out.println(findChildren(id, jsonArray));
    }

    private static String findChildren(String id, JSONArray jsonArray) {
        String path="";
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject thisNode = jsonArray.getJSONObject(i);
            String[] split = id.split(":");
            String parentId = split[0];
            String thisId = split[1];
            path=thisNode.getString("title");

            if(thisNode.getString("key")!=thisId && thisNode.getString("parentId")!=parentId){
                path=findChildren(id,thisNode.getJSONArray("children"));
            }
        }
        return path;
    }
}
